package db.test.callback;

import org.flywaydb.core.api.callback.Callback;
import org.flywaydb.core.api.callback.Context;
import org.flywaydb.core.api.callback.Event;

/**
 * flyway生命周期回调，可以捕获生命周期内的各个事件，并且自定义对应的操作
 */
public class MyNotifierCallback implements Callback {

    // 设定回调支持的事件,比如MyNotifierCallback只支持Event.AFTER_MIGRATE和Event.AFTER_MIGRATE_ERROR两种回调事件
    @Override
    public boolean supports(Event event, Context context) {
        return event.equals(Event.AFTER_MIGRATE) || event.equals(Event.AFTER_MIGRATE_ERROR);
    }

    // Not relevant if we don't interact with the database
    @Override
    public boolean canHandleInTransaction(Event event, Context context) {
        return true;
    }

    /**
     * 捕获到对应的事件后在此做对应的处理
     * @param event
     * @param context
     */
    @Override
    public void handle(Event event, Context context) {
        String notification = event.equals(Event.AFTER_MIGRATE) ? "Success" : "Failed";
        System.out.println(notification);
        // ... Notification logic ...
        //notificationService.send(notification);
    }

    @Override
    public String getCallbackName() {
        return "MyNotifier";
    }
}
